package com.example.zwh.computershop.home.views;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zwh.computershop.R;
import com.example.zwh.computershop.particulars.particulars;

import java.util.List;

public class Goods_Home_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private List<Goods> mGoods;//数据源
    private Context context;//上下文
    int item_id = -1;


    public Goods_Home_Adapter(List<Goods> mGoods, Context context) {
        this.mGoods = mGoods;
        this.context = context;
    }

    //判断输入的视图  以及视图的监听事件
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, final int i) {
        View view;
        switch (getViewType(i)) {
            case 2:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_menu1, viewGroup, false);
                final classify_ViewHolder holder2 = new classify_ViewHolder(view);
                holder2.classify_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder2.getAdapterPosition();
                        String str = mGoods.get(position).goods_name;
                        Toast.makeText(context, "点击"+str, Toast.LENGTH_SHORT).show();
                    }
                });
                return holder2;
            case 3:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_good_menu, viewGroup, false);
                final goods_ViewHolder holder3 = new goods_ViewHolder(view);
                holder3.goods_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder3.getAdapterPosition();
                        String str = mGoods.get(position).goods_name;
                        Toast.makeText(context, "点击"+str, Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(context, particulars.class);
                        intent.putExtra("come_product",str);
                        context.startActivity(intent);
                    }
                });
                return holder3;
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        Goods goods = mGoods.get(i);
        if(viewHolder instanceof goods_ViewHolder ) {
            ((goods_ViewHolder) viewHolder).img_home_goods.setImageResource(goods.getGoods_uri());
            ((goods_ViewHolder) viewHolder).tv_home_name.setText(goods.goods_name);
            ((goods_ViewHolder) viewHolder).tv_home_price.setText(Double.toString(goods.goods_price));
        } else if(viewHolder instanceof classify_ViewHolder ) {
            ((classify_ViewHolder) viewHolder).img_home_classimage.setImageResource(goods.getGoods_uri());
            ((classify_ViewHolder) viewHolder).tv_home_classname.setText(goods.goods_name);
        }
    }

    //有多少个item？
    @Override
    public int getItemCount() {
        return mGoods.size();
    }

    //item类型
    public int getViewType(int position) {
        item_id++;
//        Log.e("Shenyutian", "getViewType: id:"+item_id );
        if(item_id > 0)
            return mGoods.get(item_id).type;
        else
            return 2;
    }

    //第二种布局：分类布局
    static class classify_ViewHolder extends RecyclerView.ViewHolder{

        View classify_view;
        private ImageView img_home_classimage;
        private TextView tv_home_classname;

        public classify_ViewHolder(View view) {
            super(view);
            classify_view = view;
            img_home_classimage = view.findViewById(R.id.iv_home_menu);
            tv_home_classname = view.findViewById(R.id.tv_home_menu);
        }

    }

    //第三种布局 ： 商品详情布局
    static class goods_ViewHolder extends RecyclerView.ViewHolder{

        private View goods_view;
        private ImageView img_home_goods;
        private TextView tv_home_name;
        private TextView tv_home_price;

        public goods_ViewHolder(View view) {
            super(view);
            goods_view = view;
            img_home_goods = view.findViewById(R.id.iv_home_good_menu1);
            tv_home_name = view.findViewById(R.id.tv_home_menu1_name);
            tv_home_price = view.findViewById(R.id.tv_home_goods_price);
        }
    }

}
